package com.random.fox.whatspay.ui;

import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.random.fox.whatspay.AppContext;
import com.random.fox.whatspay.R;
import com.random.fox.whatspay.bean.Account;
import com.random.fox.whatspay.bean.Cost;
import com.random.fox.whatspay.widget.CalculatorDialog;
import com.random.fox.whatspay.widget.TimePicker;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

/**
 * 输入消费的界面
 * Created by 渠 on 2014/11/2.
 */
public class BillAty extends BaseActivity {
    private Spinner spinnerDay;
    private Spinner spinnerTime;
    private TextView cost;
    private Float result;

    private EditText remark;
    private String TAG = "BillAty";

    private Date mDate;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.bill_aty);

        preInit();
        initView();
        setListeners();
        loadDate();
    }

    @Override
    protected void initView() {
        spinnerDay = (Spinner) findViewById(R.id.spinner_day);
        spinnerTime = (Spinner) findViewById(R.id.spinner_time);
        remark = (EditText) findViewById(R.id.et_remark);

        updateTime();

        cost = (TextView) findViewById(R.id.tv_cost);

        Toolbar toolbar = (Toolbar) findViewById(R.id.tb_toolbar);
        setSupportActionBar(toolbar);

    }

    private void updateTime() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(mDate);
        SimpleDateFormat format = new SimpleDateFormat("MM月dd日");
        String[] dayString = new String[]{format.format(mDate)};

        format = new SimpleDateFormat("HH:mm");
        String amOrPm = calendar.getDisplayName(Calendar.AM_PM, Calendar.SHORT, Locale.getDefault());
        String[] timeString = new String[]{amOrPm+" "+format.format(mDate)};
        spinnerDay.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,dayString));
        spinnerTime.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,timeString));
    }

    private void setListeners(){
        cost.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final CalculatorDialog calculatorDialog = new CalculatorDialog(v.getContext());
                calculatorDialog.show();
                calculatorDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
                    @Override
                    public void onDismiss(DialogInterface dialog) {
                        result = calculatorDialog.getResult();
                        if (result.isInfinite() || result.isNaN()){
                            result = 0.0f;
                        }
                        cost.setText(result +"");
                    }
                });
            }
        });


        spinnerTime.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()){
                    case MotionEvent.ACTION_UP:
                        TimePicker timePicker = new TimePicker(v.getContext(),mDate);
                        timePicker.setTimePickListener(new TimePicker.TimePickListener() {
                            @Override
                            public void onComplete(Date date) {
                                mDate = date;
                                updateTime();
                            }

                            @Override
                            public void onCancel() {

                            }
                        });
                        timePicker.show();
                        break;
                }
                return true;
            }
        });

        spinnerDay.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()){
                    case MotionEvent.ACTION_UP:
                        Calendar calendar = Calendar.getInstance();
                        calendar.setTime(mDate);
                        DatePickerDialog datePickerDialog = new DatePickerDialog(v.getContext(),new DatePickerDialog.OnDateSetListener() {
                            @Override
                            public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                                //忽略年的设置，后续的版本中，将自定义DatePicker
                                //mDate.setYear(year);
                                mDate.setMonth(monthOfYear);
                                mDate.setDate(dayOfMonth);
                                updateTime();
                            }
                        },calendar.get(Calendar.YEAR),calendar.get(Calendar.MONTH),calendar.get(Calendar.DAY_OF_MONTH));
                        datePickerDialog.show();
                }
                return true;
            }
        });
    }

    @Override
    protected void preInit() {
        mDate = new Date();
    }

    @Override
    protected void loadDate() {

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_bill,menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();

        switch (id){
            case R.id.action_quickly:
                Toast.makeText(this,"点击了ActionBar",Toast.LENGTH_LONG).show();
                break;
            case R.id.action_ok:
                Cost cost = new Cost();
                cost.setDate(new Date());
                AppContext appContext = (AppContext) getApplicationContext();
                Account currentAccount = appContext.getCurrentAccount();
                cost.setAccount(currentAccount);
                cost.setCost(result == null ? 0 : result.longValue());
                cost.setDes(remark.getText().toString());
                cost.setDate(mDate);
                appContext.getCostDao().insert(cost);
                finish();
                break;
            case R.id.action_cancel:
                Log.d(TAG,"取消了");
                finish();
                break;
        }

        return super.onOptionsItemSelected(item);
    }
}
